
#include <stdio.h>
#include "iostream"

using namespace std;

int main(int argc, char* argv[])
{
	
	const char* _f_ = "[init]";
	const char* cfg= "./config"; 
	FILE* config = NULL; 
	FILE* block = NULL;
	
	int block_size = 0, block_num = 0;
	int i,type;
	string file_name;
	char file_num[10];
	char* zeros;
	if(argc<2){
#ifdef DEBUG
	printf("%s: Root dir isn't defined\n",_f_); 
#endif
	return 1;
	}

	config = fopen(cfg,"rb");
	
	if(config == NULL){
#ifdef PRINT_MSG
	printf("%s: cant open config file %s\n",_f_, cfg); 
#endif
	return 3;		
	}

	if(fscanf(config,"%d\n%d",&block_size,&block_num)!=2){
#ifdef PRINT_MSG
	printf("%s: config file error %s\n",_f_, cfg); 
#endif
	return 4;		
	}

	if(block_size<1024)
		block_size = 1024;
	if(block_num*block_size>50*1024*1024)
		block_num = 50*1024;
		
	zeros = new char[block_size];
	memset(zeros,0,block_size);
	for(i = 1;i<=block_num;i++)
	{
		file_name = argv[1];
		sprintf(file_num,"/.%d",i);
		file_name.append(file_num);
		
		block = fopen(file_name.data(),"w+");
		if(block == NULL)
		{

#ifdef DEBUG
			printf("%s: can't create file %s.\n",_f_,file_name.data()); return 4;
#endif
			return 1;
		}
		else
		{
			fwrite(zeros,block_size,sizeof(char),block);
			fclose(block);
		}
		
	}
	delete zeros;
	file_name = argv[1];
	file_name.append("/.1");
	block = fopen(file_name.data(),"rb+");

	type = 0;
	fwrite(&type,1,sizeof(int),block);
	fwrite(&block_size,1,sizeof(int),block);
	fwrite(&block_num,1,sizeof(int),block);
	fclose(block);	
	return 0;
}

